package com.stone.back.dish.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.stone.back.dish.entity.BfDish;
import com.stone.back.dish.entity.BfDishType;
import com.stone.base.dao.BaseDao;
import com.stone.base.entity.Page;

/**
 * 菜品管理
 * @author Stone P
 *
 */
@Repository
public class DishDao extends BaseDao implements IDishDao {
	/**
	 * 查询菜品分页信息
	 * @param pageNumber	页数
	 * @param dishChName	中文菜名
	 * @param dishEnName	英文菜名
	 * @param priceMin		单价
	 * @param priceMax		单价
	 * @param startDate		创建日期
	 * @param endDate		创建日期
	 * @return
	 */
	@Override
	public Page selectDishPage(int pageNumber, String dishChName, String dishEnName, String priceMin, String priceMax, 
			String startDate, String endDate, String dishTypeId) {
		StringBuilder hql = new StringBuilder("from BfDish where status = 1 ");
		if(StringUtils.isNotBlank(dishChName)) {
			hql.append(" and dishchname like '%" + dishChName + "%'");
		}
		if(StringUtils.isNotBlank(dishEnName)) {
			hql.append(" and dishenname like '%" + dishEnName + "%'");
		}
		if(StringUtils.isNotBlank(priceMax)) {
			hql.append(" and price <= " + priceMax);
		}
		if(StringUtils.isNotBlank(priceMin)) {
			hql.append(" and price >= " + priceMin);
		}
		if(StringUtils.isNotBlank(startDate)) {
			hql.append(" and createdate >= '" + startDate + "'");
		}
		if(StringUtils.isNotBlank(endDate)) {
			hql.append(" and createdate <= '" + endDate + "'");
		}
		if(StringUtils.isNotBlank(dishTypeId)) {
			hql.append(" and dishTypeId = " + dishTypeId);
		}
		hql.append(" order by dishid desc");
 		Page page = this.queryPage(hql.toString(), pageNumber);
		return page;
	}
	
	/**
	 * 查询菜品分类列表
	 * @return
	 */
	@Override
    public List<BfDishType> selectDishType() {
    	String hql = "from BfDishType";
		List<BfDishType> list = this.hibernateTemplate.find(hql);
		return list;
	}

	/**
	 * 增加菜品
	 * @param dish	菜品信息
	 * @return
	 */
	@Override
	public Integer addDish(BfDish dish) {
		return (Integer) this.hibernateTemplate.save(dish);
	}

	/**
	 * 修改菜品
	 * @param dish	菜品信息
	 * @return
	 */
	@Override
	public void updateDish(BfDish dish) {
		this.hibernateTemplate.update(dish);
	}

	/**
	 * 查询菜品详情
	 * @param dishid	菜品Id
	 * @return
	 */
	@Override
	public BfDish selectDishDetail(int dishId) {
		return this.hibernateTemplate.get(BfDish.class, dishId);
	}
}
